package com.huiyujinrong.qiangdanbao.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.huiyujinrong.qiangdanbao.R;
import com.huiyujinrong.qiangdanbao.bean.FragmentGrabSingleBean;
import com.huiyujinrong.qiangdanbao.utils.DataUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by ZQXQ-Developer on 2017/6/9.
 */

public class HomeAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context mContext;
    private List<FragmentGrabSingleBean.list> datas;//数据
    private List<String> idList;

    private OnRecyclerViewItemClickListener mOnItemClickListener = null;
    private static final int TYPE_ITEM = 0;
    private static final int TYPE_FOOTER = 1;


    public void setOnItemClickListener(OnRecyclerViewItemClickListener listener) {
        mOnItemClickListener = listener;
    }

    public HomeAdapter(Context mContext, List<FragmentGrabSingleBean.list> datas,List<String> idList) {
        this.mContext = mContext;
        this.datas = datas;
        this.idList=idList;
        Log.i("TAG","________(:"+idList);

    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == TYPE_ITEM) {
            View view = LayoutInflater.from(mContext).inflate(R.layout.item_grabsingle, parent, false);
            MyViewHolder holder = new MyViewHolder(view);
            //给布局设置点击和长点击监听
            return holder;
        } else {
            View view = LayoutInflater.from(mContext).inflate(R.layout.item_foot, parent,
                    false);
            return new FootViewHolder(view);
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        if (holder instanceof MyViewHolder) {

            MyViewHolder ho = (MyViewHolder) holder;
            FragmentGrabSingleBean.list list = datas.get(position);
            ho.money.setText(list.getApply_amt() == null ? "" : list.getApply_amt());
            ho.month.setText(list.getLoan_term() == null ? "" : list.getLoan_term());
            int sex = list.getSex();
            String name = list.getTrue_name();
            if (!TextUtils.isEmpty(name)) {
                String tempName = String.format("%s%s", name.substring(0, 1), sex == 1 ? "先生" : "女士");
                ho.name.setText(tempName); //姓名
            } else {
                ho.name.setText("");
            }
            if (list.getCredit_score() == 0) {
                ho.tvZhima.setVisibility(View.GONE);
            } else {
                ho.tvZhima.setVisibility(View.VISIBLE);
                ho.tvZhima.setText("芝麻分 " + list.getCredit_score());
            }

            if (list.getQuality() == 1) {
                ho.imGoods.setVisibility(View.VISIBLE);
            } else {
                ho.imGoods.setVisibility(View.GONE);
            }

            long time = list.getCreate_time() / 1000;
            ho.time.setText(DataUtils.getTimeElapse(time));  //多少时间前


            ho.city.setText(DataUtils.getStringData(list.getCity_name())); //城市
            switch (list.getLoan_type()) {
                case 1:
                    ho.type.setText("工薪族");
                    ho.yueMoney.setText("月收入 " + list.getMonth_income() + "元");
                    break;
                case 2:
                    ho.type.setText("企业主");
                    ho.yueMoney.setText("对公流水 " + list.getBank_dgls() + "万元");   //月收入
                    break;
            }

            String birthDath = list.getBirth_date();
            if (!TextUtils.isEmpty(birthDath)) {
                int oldYear = resolveDateString2Year(list.getBirth_date() == null ? "" : list.getBirth_date());
                int newYear = getCurrentYear();
                ho.age.setText(String.valueOf(newYear - oldYear) + "岁");  //年龄
            } else {
                ho.age.setText("0岁");  //年龄
            }


            if (datas.get(position).isRead()){
                ho.isRead.setVisibility(View.VISIBLE);
            }else {
                ho.isRead.setVisibility(View.GONE);
            }



            //房产状况
            switch (list.getHouse_state()) {
                case 1:
                    ho.isFang.setText("自置无按揭");
                    break;
                case 2:
                    ho.isFang.setText("自置有按揭");
                    break;
                case 3:
                    ho.isFang.setText("亲属楼宇");
                    break;
                case 4:
                    ho.isFang.setText("集体宿舍");
                    break;
                case 5:
                    ho.isFang.setText("租房");
                    break;
                case 6:
                    ho.isFang.setText("自建房");
                    break;
                case 7:
                    ho.isFang.setText("共有");
                    break;
                case 8:
                    ho.isFang.setText("有商铺/商住两用");
                    break;
                case 9:
                    ho.isFang.setText("其他");
                    break;
            }
            //车状况
            switch (list.getCar_state()) {
                case 1:
                    ho.isCar.setText("无车产");
                    break;
                case 2:
                    ho.isCar.setText("有车产无抵押");
                    break;
                case 3:
                    ho.isCar.setText("有车产无抵押");
                    break;
            }
//            if(idList.size()>0&&idList!=null){
//                for (int i=0;i<idList.size();i++){
//                    if(idList.get(i).contains(datas.get(position).getId())) {
//                        ho.isRead.setVisibility(View.VISIBLE);
//                    }else {
//                        ho.isRead.setVisibility(View.GONE);
//                    }
//                }
//
//            }else {
//                ho.isRead.setVisibility(View.GONE);
//            }



            //抢单状态：1:立即抢单2：被抢
            switch (list.getLoan_status()) {
                case 1:
                    ho.qiangDan.setVisibility(View.VISIBLE);  // 立即抢单
                    ho.beiQiang.setVisibility(View.GONE);
                    break;
                case 2:
                    ho.beiQiang.setVisibility(View.VISIBLE);  // 已被抢单
                    ho.qiangDan.setVisibility(View.GONE);
                    break;
            }

            ho.relativeLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (null != mOnItemClickListener) {
                        mOnItemClickListener.onItemClick(position);
                    }
                }
            });
        } else {

        }
    }


    private int resolveDateString2Year(String date) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date oldDate = null;
        try {
            oldDate = simpleDateFormat.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(oldDate);
        return calendar.get(Calendar.YEAR);
    }

    private int getCurrentYear() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date(System.currentTimeMillis()));
        return calendar.get(Calendar.YEAR);
    }


    @Override
    public int getItemCount() {
        return datas == null ? 0 : datas.size();
    }

    @Override
    public int getItemViewType(int position) {
        if (position == getItemCount()) {
            return TYPE_FOOTER;
        } else {
            return TYPE_ITEM;
        }
    }


    static class FootViewHolder extends RecyclerView.ViewHolder {
        public FootViewHolder(View view) {
            super(view);
        }
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.gs_money)
        TextView money;
        @BindView(R.id.gs_month)
        TextView month;
        @BindView(R.id.gs_name)
        TextView name;
        @BindView(R.id.gs_time)
        TextView time;
        @BindView(R.id.gs_city)
        TextView city;
        @BindView(R.id.gs_type)
        TextView type;
        @BindView(R.id.gs_age)
        TextView age;
        @BindView(R.id.gs_xueli)
        TextView xueli;
        @BindView(R.id.gs_yueInputMoney)
        TextView yueMoney;
        @BindView(R.id.gs_isFang)
        TextView isFang;
        @BindView(R.id.gs_isCar)
        TextView isCar;

        @BindView(R.id.gs_beiQiang)
        ImageView beiQiang;
        @BindView(R.id.gs_qiangDan)
        ImageView qiangDan;

        @BindView(R.id.rlParent)
        LinearLayout relativeLayout;

        @BindView(R.id.tvZhima)
        TextView tvZhima;//芝麻分

        @BindView(R.id.home_isGood)
        ImageView imGoods;//优质订单

        @BindView(R.id.image_isRead)
        ImageView isRead;

        public MyViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    //自定义监听事件
    public static interface OnRecyclerViewItemClickListener {
        void onItemClick(int pos);

    }
}
